package com.tyt.lelan.utils;

import java.util.ArrayList;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.ContentValues;
import android.content.Context;
import android.text.Html;
import android.util.Log;

import com.tyt.lelan.entity.RssItem;
import com.tyt.lelan.provider.FeedsData.EntryColumns;

public class RssParser {
	private String urlLink;
	private Context mContext;
	private Date timeBase;
	private int    rssId;
	
	public RssParser(String link,Context context,Date timeBase,int rssid) {
		this.urlLink=link;
		this.mContext=context;
		this.timeBase=timeBase;
		this.rssId=rssid;
	}
	
	public RssParser(String link) {
		this.urlLink=link;
	}
	
	
	public ArrayList<RssItem> parserNotStore() throws Exception {
		ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
            String sourceString =Net.getInstance().get(urlLink);
    		if(!rssItems.isEmpty())rssItems.clear();
			Document doc=Jsoup.parse(sourceString);
			Elements Elements_rssItems=doc.select("item");
			int len=Elements_rssItems.size();
			for(int i=0;i<len;i++){				
				RssItem temp=new RssItem();
				Element element_item_rss=Elements_rssItems.get(i);
    			temp.setTitle(Html.fromHtml(element_item_rss.select("title").first().text()).toString());
    			Log.v("", element_item_rss.select("pubDate").first().text());
    			temp.setLink(element_item_rss.select("link").first().text());
    			temp.setTime(element_item_rss.select("pubDate").first().text());
				temp.setDescription(element_item_rss.select("description").first().text());
				rssItems.add(temp);    			
			}
		   return rssItems;
	}
	
	public boolean parser() throws Exception {
		
		    Log.i("RSSID", ""+rssId);
        	//打开连接，获取原html
            String sourceString =Net.getInstance().get(urlLink);
            boolean isUpdate=false;
			Document doc=Jsoup.parse(sourceString);

			Elements Elements_rssItems=doc.select("item");
			int len=Elements_rssItems.size();
			for(int i=0;i<len;i++){
				
				Element element_item_rss=Elements_rssItems.get(i);

	  			//获取Time的内容
				Date pubDate=PubDateParser.parse(element_item_rss.select("pubDate").first().text());
				
				
				if(timeBase==null||pubDate.after(timeBase)){
					 
					Log.i("=RssParser=", "Insert");
					//跟基准时间比较,若该条信息的时间晚于基准时间，则插入
					insertRssItemToEntries(element_item_rss);
					
					isUpdate=true;
				}
				else{					
					break;
				}	
			}
		  return isUpdate;
	}
	
	private void insertRssItemToEntries(Element item){
		
		ContentValues values=new ContentValues();
		values.put(EntryColumns.DESCRIPTION, item.select("description").first().text());
		values.put(EntryColumns.PUBDATE, item.select("pubDate").first().text());
		values.put(EntryColumns.TITLE, item.select("title").first().text());
		values.put(EntryColumns.RSS_ID, this.rssId);
		values.put(EntryColumns.LINK, this.urlLink);
		
		this.mContext.getContentResolver().insert(EntryColumns.CONTENT_URI, values);
	}

	
}


